// pages/getDetail/getDetail.js
let videoAd = null
let db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    getDetail:{},
    tab:1,
    isUserLogin:false,
    openId:'',
    type:''
  },
  listLink(){
    wx.navigateTo({
      url: '/pages/moneyList/moneyList',
    })
  },
  gengxin(){
    let that = this
    wx.showLoading({
      title: '请稍等...'
    })
    db.collection('moneyList')
    .where({
      type: that.data.type/1
    })
    .get()
    .then((e)=>{
      console.log(e)
      let openId = that.data.openId
      if(e.data[0].user.length==e.data[0].count){
        wx.hideLoading({
          success: (res) => {},
        })
        wx.showModal({
          confirmText: '确定',
          content: '活动参与人数已满~~',
          showCancel: false
        })
      } else {
        let getDetail = e.data[0]
        getDetail.user.unshift({name:that.data.name,icon:that.data.avatarUrl,openId})
        let userInfo = wx.getStorageSync('userInfo')
        let zhongjiang = getDetail.user.length==getDetail.count&&getDetail.zhongjiang.length==0?[getDetail.user[Math.floor(Math.random()*getDetail.user.length)]]:[]
        if(zhongjiang.length>0){
          zhongjiang[0].wx= ''
          zhongjiang[0].isConfirm= false
        }
        db.collection('moneyList')
        .where({
          type: that.data.type/1
        })
        .update({
          // data 传入需要局部更新的数据
          data: {
            user:db.command.unshift({name:that.data.name,icon:that.data.avatarUrl,openId})
          }
        })
        .then(e=>{
          wx.hideLoading({
            success: (res) => {},
          })
          wx.showToast({
            title: '助力成功',
            duration: 1000
          })
          userInfo.zhuli = userInfo.zhuli-1
          that.setData({
            userInfo
          })
          wx.setStorageSync('userInfo', userInfo)
          db.collection('user')
          .where({
            openId: wx.getStorageSync('openId')
          })
          .update({
            data: {
              userInfo:{
                zhuli:userInfo.zhuli
              }
            }
          })
        })
        .catch(e=>{
          console.log(e)
          wx.hideLoading({
            success: (res) => {},
          })
          that.setData({
            getDetail
          })
          wx.showModal({
            confirmText: '确定',
            content: '助力失败，请检查网络，或稍后再试',
            showCancel: false
          })
        })
      }
    })
    .catch((e)=>{
      console.log(e)
      wx.hideLoading({
        success: (res) => {},
      })
      wx.showModal({
        confirmText: '确定',
        content: '参加助力失败，请检查网络，或稍后再试',
        showCancel: false
      })
    })
  },
  isUserLogin(){
    let isUserLogin
    if(wx.getStorageSync('userInfo')){
      isUserLogin=true
    } else {
      isUserLogin=false
    }
    this.setData({isUserLogin,userInfo:wx.getStorageSync('userInfo')})
    wx.hideLoading({
      success: (res) => {},
    })
  },
  watchAll(){
    wx.setStorageSync('userList', this.data.getDetail.user)
    wx.navigateTo({
      url: '/pages/userList/userList',
    })
  },
  tabChoose(e){
    this.setData({
      tab:e.currentTarget.dataset.index
    })
  },
  choujiang(){
    if(wx.getStorageSync('userInfo').zhuli === 0){
      wx.showModal({
        confirmText: '确定',
        content: '今日的助力次数已用完，请明天继续帮好友助力',
        showCancel: false
      })
      return
    }
    console.log(videoAd)
    wx.showLoading({
      title: '请稍等...',
      mask:true
    })
    if (videoAd) {
      videoAd.show()
        .then(() => {
          wx.hideLoading({
            success: (res) => {},
          })
        })
        .catch(() => {
          wx.hideLoading({
            success: (res) => {},
          })
          wx.showToast({
            title: '请5s后重试',
            duration:2000
          })
        })
    } else {
      wx.hideLoading({
        success: (res) => {},
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    getApp().getRandomAd(true,this)
    setTimeout(function(){
        that.getRandomAd()
        that.getRandomAd(true)
        getApp().chaping()
    },300)
    that.setData(options)
    console.log(that.data)
    if (wx.createRewardedVideoAd) {
      videoAd = wx.createRewardedVideoAd({
        adUnitId: getApp().globalData.jili
      })
      videoAd.onLoad(() => { })
      videoAd.onError((err) => { })
      videoAd.onClose((status) => { 
        if (status && status.isEnded || status === undefined) {
          // 正常播放结束，下发奖励
          // continue you code
          that.gengxin()
          console.log('已完成')
        } else {
          console.log('未完成')
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    getApp().WX_LOGIN(function(){
      that.isUserLogin()
    })
    this.getRandomAd()
    getApp().chaping()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }

  
})